import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 * 
 */

/**
 * @author sdvina
 * 
 */
@SuppressWarnings("serial")
public class MyMainFrame extends JFrame implements ActionListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	MyCanvas myCanvas;
	JPanel panelNorth, panelPreview;
	JButton start, preview, set;
	Container container;

	public MyMainFrame() {
		container = this.getContentPane();
		start = new JButton("开始");
		preview = new JButton("预览");
		set = new JButton("设置");
		start.addActionListener(this);
		preview.addActionListener(this);
		set.addActionListener(this);

		panelPreview = new JPanel();
		panelPreview.setLayout(null);
		Icon icon = new ImageIcon(MyMainFrame.class.getResource("picture/pic_"
				+ MyCanvas.pictureID + ".jpg"));
		JLabel label = new JLabel(icon);
		label.setBounds(0, 0, 300, 300);
		panelPreview.add(label);

		panelNorth = new JPanel();
		panelNorth.setBackground(Color.WHITE);
		panelNorth.add(start);
		panelNorth.add(preview);
		panelNorth.add(set);
		myCanvas = new MyCanvas();
		container.add(myCanvas, BorderLayout.CENTER);
		container.add(panelNorth, BorderLayout.NORTH);
		this.setTitle("Puzzles Game");
		this.setLocation(300, 200);
		this.setSize(308, 365);
		this.setResizable(false);
		this.setDefaultCloseOperation(3);
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyMainFrame frame = new MyMainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		JButton button = (JButton) arg0.getSource();
		if (button == start) {
			myCanvas.Start();
		} else if (button == preview) {
			if (button.getText() == "预览") {
				container.remove(myCanvas);
				container.add(panelPreview);
				panelPreview.updateUI();
				container.repaint();
				button.setText("返回");
			} else {
				container.remove(panelPreview);
				container.add(myCanvas);
				container.repaint();
				button.setText("预览");
			}
		} else if (button == set) {
			JComboBox<String> pic = new JComboBox<String>();
			pic.addItem("美美");
			pic.addItem("一灯");
			pic.addItem("风景");
			int i = JOptionPane.showConfirmDialog(this, pic, "选择图片",
					JOptionPane.OK_CANCEL_OPTION);
			if (i == JOptionPane.YES_OPTION) {
				MyCanvas.pictureID = pic.getSelectedIndex() + 1;
				// 让恢复到起初有序状态
				myCanvas.rollBack();
				myCanvas.reloadPicture();
				Icon icon = new ImageIcon(
						MyMainFrame.class.getResource("picture/pic_"
								+ MyCanvas.pictureID + ".jpg"));
				JLabel label = new JLabel(icon);
				label.setBounds(0, 0, 300, 300);
				panelPreview.removeAll();
				panelPreview.add(label);
				panelPreview.repaint();

			}
		}
	}

}
